

//
//  MyPriseViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/24.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "MyPriseViewController.h"
#import "MyPriseTableViewCell.h"
#import "AppUtils.h"
#import "MyPriseM.h"

@interface MyPriseViewController ()<UITableViewDataSource,UITableViewDelegate>
{

    __weak IBOutlet UILabel *_allnumLabel;
    __weak IBOutlet UITableView *_mpTv;
    NSMutableArray *_dataAry;
    NSMutableArray *_heghtAry;
    NSArray *_askStyAry;
    NSArray *_askStyImageAry;
    NSArray *_trustAry;
}
@end

@implementation MyPriseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    // Do any additional setup after loading the view.
    _mpTv.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"对我的评价"];
_askStyAry=@[@"免费咨询",@"在线问诊",@"电话问诊",@"门诊加号"];
    _askStyImageAry=@[@"在线问诊1",@"在线问诊1",@"电话问诊1",@"门诊加号1"];
    _trustAry=@[@"信任",@"一般",@"不信任"];
    self.view.backgroundColor = [AppUtils getBackground];
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
//请求数据
    [self reloadtableView];
}
-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)reloadtableView
{
    NSString *docId= [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [MyPriseM requetDoctorPriseWith:docId success:^(NSArray *priseAry) {
        _dataAry=[NSMutableArray arrayWithArray:priseAry];
        _allnumLabel.text=[NSString stringWithFormat:@"  共%lu条评论",(unsigned long)_dataAry.count];
        _heghtAry=[NSMutableArray array];
        for(int i=0;i<_dataAry.count;i++)
        {
            MyPriseM *myprise=_dataAry[i];
            NSNumber *num=[NSNumber numberWithFloat:[self labelwidthWith:myprise.doctor_evaluate_content]];
            [_heghtAry addObject:num];
            
        }
        [_mpTv reloadData];
      
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
-(CGFloat)labelwidthWith:(NSString *)str
{
    
    UIFont *font= [UIFont systemFontOfSize:17.0];
    NSDictionary *fontDic = @{NSFontAttributeName:font};
    
    CGRect labelRect = [str boundingRectWithSize:CGSizeMake(kScreenWidth-20,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    CGSize size = labelRect.size;
    return size.height;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *num=_heghtAry[indexPath.row];
   
    return (num.floatValue+89);
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 40;
//}
//-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 25, kScreenWidth-10,15 )];
//    label.backgroundColor=[UIColor clearColor];
//    label.textColor=[UIColor blackColor];
//    label.font=[UIFont systemFontOfSize:16];
//    label.tintAdjustmentMode=NSTextAlignmentLeft;
//    label.text=[NSString stringWithFormat:@"  共%lu条评论",(unsigned long)_dataAry.count];
//    return label;
//}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    MyPriseM *myprise=_dataAry[indexPath.row];
    MyPriseTableViewCell *myPriseCell=[tableView dequeueReusableCellWithIdentifier:@"theEvaluationofme" forIndexPath:indexPath];
    myPriseCell.selectionStyle= UITableViewCellSelectionStyleNone;
    myPriseCell.prise.text=myprise.doctor_evaluate_content;
   
    if(indexPath.row==_dataAry.count-1)
    { NSNumber *num=_heghtAry[indexPath.row];
        
        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,num.floatValue+88, kScreenWidth, 1)];
        view.backgroundColor=[UIColor colorWithRed:138/255.0 green:127/255.0 blue:118/255.0 alpha:1];
        [myPriseCell addSubview:view];
    }
    myPriseCell.trustLabel.text=_trustAry[myprise.doctor_evaluate_level.intValue-1];
    myPriseCell.questiontype.text=_askStyAry[myprise.doctor_evaluate_type.intValue];
    myPriseCell.qtImageView.image=[UIImage imageNamed:_askStyImageAry[myprise.doctor_evaluate_type.intValue]];

//    for(int i=0;i<_askStyAry.count;i++)
//    {
//        if(i<=2)
//        {if([myprise.doctor_evaluate_level isEqualToString:[NSString stringWithFormat:@"%d",i+1]])
//            myPriseCell.trustLabel.text=_trustAry[i];
//            
//        }
//        if([myprise.doctor_evaluate_type isEqualToString:[NSString stringWithFormat:@"%d",i]])
//        {
//            myPriseCell.questiontype.text=_askStyAry[i];
//            myPriseCell.qtImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@%d",_askStyAry[i],1]];
//            
//        }
//        
//    }
    myPriseCell.customerName.text=[NSString stringWithFormat:@"%@  %@****%@",myprise.doctor_evaluate_name,[myprise.user_phone substringWithRange:NSMakeRange(0,3)],[myprise.user_phone substringWithRange:NSMakeRange(6,4)]];
    myPriseCell.dateLabel.text=myprise.doctor_evaluate_date;
       cell=myPriseCell;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
